# _*_ coding:utf-8 _*_
# @Time  : 2023.06.08
# @Author: zizlee
import pathlib
import string
import random
from settings import FILE_STORAGE


def generate_unique_fileinfo(file_folder, filename, suffix, hashed=False):
    if hashed:
        filename = ''.join(random.sample(string.ascii_letters, 16))
    abs_folder = pathlib.Path(FILE_STORAGE).joinpath(file_folder)
    if not abs_folder.exists():
        abs_folder.mkdir(parents=True)
    abs_filepath = abs_folder.joinpath("{}{}".format(filename, suffix))
    if abs_filepath.exists():
        new_filename_suffix = ''.join(random.sample(string.ascii_letters, 6))
        new_filename = "{}_{}".format(filename, new_filename_suffix)
        return generate_unique_fileinfo(file_folder, new_filename, suffix)
    else:
        return {
            'folder': file_folder,
            'filepath': abs_filepath,
            'filename': filename,
            'suffix': suffix,
        }

